/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import HTTPDownloader.HTTPTaskManager;
import HTTPDownloader.Status;
import Tools.AppDataManager;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author patricksamson236
 */
public class TableTaskManager {

    ArrayList<HTTPTaskManager> http_tasks = new ArrayList<HTTPTaskManager>();
    JTable table;
    DefaultTableModel model;

    public TableTaskManager(JTable table) {
        this.table = table;
        this.model = (DefaultTableModel) table.getModel();
    }

    public void addHttpTask(HTTPTaskManager task) {
        http_tasks.add(task);
        Object[] data = new Object[1];
        data[0] = task.getTaskName();
        //model.addRow(new Object[]{task.getTaskName()});
        model.addRow(data);
        if (task.getTaskStatus() == Status.DONE) {
            updateTaskUIOnce((String) data[0]);
        }
    }

    public ArrayList<HTTPTaskManager> getHttpTasksList() {
        return http_tasks;
    }

    public HTTPTaskManager getHttpTask(String taskname) {
        int pos = -1;
        for (int i = 0; i < http_tasks.size(); i++) {
            if (http_tasks.get(i).getTaskName().equals(taskname)) {
                pos = i;
                i = http_tasks.size() + 1;
            }
        }
        return http_tasks.get(pos);
    }

    public HTTPTaskManager getHttpTask(int row) {
        return getHttpTask(getTaskName(row));
    }

    public void removeHTTPTask(int row) {
        if (row != -1) {
            HTTPTaskManager task = getHttpTask(row);
            task.stopHTTPTask();http_tasks.remove(task);
            model.removeRow(row);
            spidercore.SpiderCore.datamanager.removeTask(task.getTaskName());
        }
    }
    
    public void removeHTTPTask(HTTPTaskManager task){
        task.stopHTTPTask();
        http_tasks.remove(task);
            model.removeRow(getTaskRow(task.getTaskName()));
    }

    public int getTaskRow(String taskname) {
        int row = -1;
        for (int i = 0; i < http_tasks.size(); i++) {
            if (taskname.equals(model.getValueAt(i, 0))) {
                row = i;
                i = http_tasks.size() + 1;
            }
        }
        return row;
    }

    public String getTaskName(int row) {
        return (String) model.getValueAt(row, 0);
    }

    public void saveHttpTasks() {
        AppDataManager adm = spidercore.SpiderCore.datamanager;
        for (int i = 0; i < http_tasks.size(); i++) {
            adm.saveHTTPTask(http_tasks.get(i).getTaskName(),
                    http_tasks.get(i).saveHTTPTask());
        }
    }

    public void updateTaskUIOnce(String taskname) {
        System.out.println("Updating once : " + taskname);
        getHttpTask(taskname).updateUIOnce();
    }

    public void updateTaskUIOnce(int row) {
        updateTaskUIOnce(getTaskName(row));
    }

    public void stopHttpTask(String taskname) {
        getHttpTask(taskname).stopHTTPTask();
    }

    public void stopHttpTask(int row) {
        stopHttpTask(getTaskName(row));
    }
}
